package com.ruoyi.reportsnhnt.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.report_gc.domain.bo.GcjxljjtbgBsInfoBo;
import com.ruoyi.report_tg.domain.vo.TgsybgBaDetailVo;
import com.ruoyi.syrw.annotation.FIllBasicInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.reportsnhnt.domain.bo.ReportSnhntNjsjbgBsInfoBo;
import com.ruoyi.reportsnhnt.domain.vo.ReportSnhntNjsjbgBsInfoVo;
import com.ruoyi.reportsnhnt.domain.ReportSnhntNjsjbgBsInfo;
import com.ruoyi.reportsnhnt.mapper.ReportSnhntNjsjbgBsInfoMapper;
import com.ruoyi.reportsnhnt.service.IReportSnhntNjsjbgBsInfoService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 混凝土凝结时间试验检测报告Service业务层处理
 *
 * @author zxs
 * @date 2024-08-12
 */
@RequiredArgsConstructor
@Service
public class ReportSnhntNjsjbgBsInfoServiceImpl implements IReportSnhntNjsjbgBsInfoService {

    private final ReportSnhntNjsjbgBsInfoMapper baseMapper;

    /**
     * 查询混凝土凝结时间试验检测报告
     */
    @Override
    @FIllBasicInfo
    public ReportSnhntNjsjbgBsInfoVo queryById(Long excelId){
        return baseMapper.selectVoById(excelId);
    }

    /**
     * 查询混凝土凝结时间试验检测报告列表
     */
    @Override
    public TableDataInfo<ReportSnhntNjsjbgBsInfoVo> queryPageList(ReportSnhntNjsjbgBsInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ReportSnhntNjsjbgBsInfo> lqw = buildQueryWrapper(bo);
        Page<ReportSnhntNjsjbgBsInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询混凝土凝结时间试验检测报告列表
     */
    @Override
    public List<ReportSnhntNjsjbgBsInfoVo> queryList(ReportSnhntNjsjbgBsInfoBo bo) {
        LambdaQueryWrapper<ReportSnhntNjsjbgBsInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ReportSnhntNjsjbgBsInfo> buildQueryWrapper(ReportSnhntNjsjbgBsInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ReportSnhntNjsjbgBsInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getPage() != null, ReportSnhntNjsjbgBsInfo::getPage, bo.getPage());
        lqw.eq(bo.getTotalPage() != null, ReportSnhntNjsjbgBsInfo::getTotalPage, bo.getTotalPage());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancdwmc()), ReportSnhntNjsjbgBsInfo::getJiancdwmc, bo.getJiancdwmc());
        lqw.eq(StringUtils.isNotBlank(bo.getBaogbh()), ReportSnhntNjsjbgBsInfo::getBaogbh, bo.getBaogbh());
        lqw.eq(StringUtils.isNotBlank(bo.getShigwtdw()), ReportSnhntNjsjbgBsInfo::getShigwtdw, bo.getShigwtdw());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcmc()), ReportSnhntNjsjbgBsInfo::getGongcmc, bo.getGongcmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcbwyt()), ReportSnhntNjsjbgBsInfo::getGongcbwyt, bo.getGongcbwyt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpxx()), ReportSnhntNjsjbgBsInfo::getYangpxx, bo.getYangpxx());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyj()), ReportSnhntNjsjbgBsInfo::getJiancyj, bo.getJiancyj());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyj()), ReportSnhntNjsjbgBsInfo::getPandyj, bo.getPandyj());
        lqw.eq(StringUtils.isNotBlank(bo.getZhuyyqsbmcjbh()), ReportSnhntNjsjbgBsInfo::getZhuyyqsbmcjbh, bo.getZhuyyqsbmcjbh());
        lqw.eq(StringUtils.isNotBlank(bo.getWeitbh()), ReportSnhntNjsjbgBsInfo::getWeitbh, bo.getWeitbh());
        lqw.eq(StringUtils.isNotBlank(bo.getJianclb()), ReportSnhntNjsjbgBsInfo::getJianclb, bo.getJianclb());
        lqw.eq(StringUtils.isNotBlank(bo.getHunntzl()), ReportSnhntNjsjbgBsInfo::getHunntzl, bo.getHunntzl());
        lqw.eq(bo.getShiyrq() != null, ReportSnhntNjsjbgBsInfo::getShiyrq, bo.getShiyrq());
        lqw.eq(StringUtils.isNotBlank(bo.getKangsdj()), ReportSnhntNjsjbgBsInfo::getKangsdj, bo.getKangsdj());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancxm()), ReportSnhntNjsjbgBsInfo::getJiancxm, bo.getJiancxm());
        lqw.eq(StringUtils.isNotBlank(bo.getJiszb()), ReportSnhntNjsjbgBsInfo::getJiszb, bo.getJiszb());
        lqw.eq(StringUtils.isNotBlank(bo.getShebIds()), ReportSnhntNjsjbgBsInfo::getShebIds, bo.getShebIds());
        lqw.eq(StringUtils.isNotBlank(bo.getBgFlag()), ReportSnhntNjsjbgBsInfo::getBgFlag, bo.getBgFlag());
        lqw.eq(StringUtils.isNotBlank(bo.getFujsm()), ReportSnhntNjsjbgBsInfo::getFujsm, bo.getFujsm());
        lqw.eq(StringUtils.isNotBlank(bo.getJianc()), ReportSnhntNjsjbgBsInfo::getJianc, bo.getJianc());
        lqw.eq(StringUtils.isNotBlank(bo.getJil()), ReportSnhntNjsjbgBsInfo::getJil, bo.getJil());
        lqw.eq(StringUtils.isNotBlank(bo.getFuh()), ReportSnhntNjsjbgBsInfo::getFuh, bo.getFuh());
        lqw.eq(bo.getRiq() != null, ReportSnhntNjsjbgBsInfo::getRiq, bo.getRiq());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancjg()), ReportSnhntNjsjbgBsInfo::getJiancjg, bo.getJiancjg());
        lqw.eq(StringUtils.isNotBlank(bo.getJiegpd()), ReportSnhntNjsjbgBsInfo::getJiegpd, bo.getJiegpd());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancjl()), ReportSnhntNjsjbgBsInfo::getJiancjl, bo.getJiancjl());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyjIds()), ReportSnhntNjsjbgBsInfo::getJiancyjIds, bo.getJiancyjIds());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyjIds()), ReportSnhntNjsjbgBsInfo::getPandyjIds, bo.getPandyjIds());
        lqw.eq(StringUtils.isNotBlank(bo.getQiangddj()), ReportSnhntNjsjbgBsInfo::getQiangddj, bo.getQiangddj());
        lqw.eq(StringUtils.isNotBlank(bo.getGuigcc()), ReportSnhntNjsjbgBsInfo::getGuigcc, bo.getGuigcc());
        lqw.eq(StringUtils.isNotBlank(bo.getYanghtj()), ReportSnhntNjsjbgBsInfo::getYanghtj, bo.getYanghtj());
        return lqw;
    }

    /**
     * 新增混凝土凝结时间试验检测报告
     */
    @Override
    public Boolean insertByBo(ReportSnhntNjsjbgBsInfoBo bo) {
        ReportSnhntNjsjbgBsInfo add = BeanUtil.toBean(bo, ReportSnhntNjsjbgBsInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
        }
        return flag;
    }

    /**
     * 修改混凝土凝结时间试验检测报告
     */
    @Override
    public Boolean updateByBo(ReportSnhntNjsjbgBsInfoBo bo) {
        // 增加对 是否合格 的填充
        fillExcelResult(bo);
        ReportSnhntNjsjbgBsInfo update = BeanUtil.toBean(bo, ReportSnhntNjsjbgBsInfo.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    private void fillExcelResult(ReportSnhntNjsjbgBsInfoBo bo) {
//        if (CollUtil.isNotEmpty(bo.getBaDetailVos())) {
//            for (TgsybgBaDetailVo detailVo : bo.getBaDetailVos()) {
//                if ("不符合".equals(detailVo.getJiegpd())) {
//                    bo.setExcelResult(2);
//                    return;
//                }
//            }
//            bo.setExcelResult(1);
//        }
        bo.setExcelResult(1);
    }

    public boolean isWell(Long excelId) {
        return baseMapper.selectVoById(excelId).getExcelResult() != 2;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ReportSnhntNjsjbgBsInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除混凝土凝结时间试验检测报告
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
